package iask.xiao.service.imp;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import iask.xiao.mapper.SysRoleMapper;
import iask.xiao.mapper.SysUserMapper;
import iask.xiao.mapper.SysUserRoleMapper;
import iask.xiao.model.SysRole;
import iask.xiao.model.SysUser;
import iask.xiao.model.SysUserExample;
import iask.xiao.model.SysUserRole;
import iask.xiao.model.SysUserRoleExample;
import iask.xiao.service.SysUserService;

@Service
public class SysUserServiceImp implements SysUserService {

	@Autowired
	private SysUserMapper userMapper;
	private SysUserRoleMapper userRoleMapper;
	private SysRoleMapper roleMapper;
	@Override
	public SysUser insert(SysUser entity) {
		// TODO Auto-generated method stub
		userMapper.insert(entity);
		return null;
	}

	@Override
	public void insertList(List<SysUser> entities) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean removeByPrimaryKey(Long primaryKey) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean update(SysUser entity) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateSelective(SysUser entity) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public SysUser getByPrimaryKey(Long primaryKey) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public SysUser getOneByEntity(SysUser entity) {
		 Assert.notNull(entity, "User不可为空！");
	        SysUser sysUser = userMapper.selectByPrimaryKey(null);
	        return null == sysUser ? null : sysUser;
	}

	@Override
	public List<SysUser> listAll() {
		SysUserExample example = new SysUserExample();
		example.setOrderByClause("username asc");
		return userMapper.selectByExample(example);
	}

	@Override
	public List<SysUser> listByEntity(SysUser entity) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public SysUser getSysUserByName(String name) {
		return userMapper.selectByName(name);
	}

	@Override
	public List<SysRole> getRoleListByUserId(Long userid) {
		SysUserRoleExample example = new SysUserRoleExample();
		example.or().andUserIdEqualTo(userid);
		List<SysUserRole> userRoleList = userRoleMapper.selectByExample(example);
		List<SysRole> roleList = new ArrayList<SysRole>();
		for(SysUserRole userrole : userRoleList){
			SysRole role = roleMapper.selectByPrimaryKey(userrole.getId());
			roleList.add(role);
		}
		return roleList;
	}

	
}
